package com.white.note.fragment;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.widget.LinearLayoutCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.white.note.CreateNoteActivity;
import com.white.note.NoteDetailActivity;
import com.white.note.R;
import com.white.note.adapter.NoteListAdapter;
import com.white.note.base.BaseFragment;
import com.white.note.db.NoteDbHelper;
import com.white.note.entity.NoteInfo;
import com.white.note.entity.UserInfo;

import java.util.List;


public class HomeFragment extends BaseFragment {
    private RecyclerView recyclerView;

    private NoteListAdapter mListAdapter;
    private LinearLayoutCompat ll_empty;

    private String[] item1 = new String[]{"置顶", "编辑", "删除"};
    private String[] item2 = new String[]{"取消置顶", "编辑", "删除"};

    private int currentIndex = 0;

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == 1000) {
            initData();
        }
    }

    @Override
    protected int getLayoutResid() {
        return R.layout.fragment_home;
    }

    @Override
    protected void initViews() {
        recyclerView = rootView.findViewById(R.id.recyclerView);
        ll_empty = rootView.findViewById(R.id.ll_empty);

        // 初始化适配器
        mListAdapter = new NoteListAdapter();
        // 设置适配器
        recyclerView.setAdapter(mListAdapter);
        // recyclerView点击事件
        mListAdapter.setOnItemClickListener(new NoteListAdapter.onItemClickListener() {
            @Override
            public void onItemClick(int position, NoteInfo noteInfo) {
                Intent intent = new Intent(getActivity(), NoteDetailActivity.class);
                intent.putExtra("noteInfo", noteInfo);
                startActivity(intent);
            }

            @Override
            public void onMoreClick(int position, NoteInfo noteInfo) {
                new AlertDialog.Builder(getActivity())
                        .setTitle("操作")
                        .setNegativeButton("取消", null)
                        .setSingleChoiceItems(noteInfo.getNote_is_top() == 0 ? item1 : item2, 0, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                currentIndex = which;
                            }
                        })
                        .setPositiveButton("确认", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                if (currentIndex == 0) {
                                    NoteDbHelper.getInstance(getActivity()).updateNoteTop(noteInfo.getNote_id(), noteInfo.getNote_is_top() == 0 ? 1 : 0);
                                    initData();

                                } else if (currentIndex == 1) {
                                    Intent intent = new Intent(getActivity(), CreateNoteActivity.class);
                                    intent.putExtra("noteInfo", noteInfo);
                                    startActivityForResult(intent, 1000);
                                } else {
                                    int row = NoteDbHelper.getInstance(getActivity()).deleteNote(noteInfo.getNote_id());
                                    if (row > 0) {
                                        Toast.makeText(getActivity(), "删除成功~~", Toast.LENGTH_SHORT).show();
                                        initData();
                                    } else {
                                        Toast.makeText(getActivity(), "删除失败~~", Toast.LENGTH_SHORT).show();
                                    }
                                }
                            }
                        })
                        .show();
                currentIndex = 0;
            }
        });
    }

    @Override
    protected void setListener() {
        // 返回跳转
        rootView.findViewById(R.id.btn_create_note).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 跳转到笔记创建页面
                // startActivity(new Intent(getActivity(), CreateNoteActivity.class));
                Intent intent = new Intent(getActivity(), CreateNoteActivity.class);
                startActivityForResult(intent, 1000);
            }
        });
    }

    @Override
    protected void initData() {
        UserInfo userInfo = UserInfo.getsUserInfo();
        List<NoteInfo> noteInfoList = NoteDbHelper.getInstance(getActivity()).queryNoteListDate(userInfo.getUsername());
        if (mListAdapter != null) {
            mListAdapter.setNoteInfoList(noteInfoList);
        }
        if (noteInfoList.size() == 0) {
            ll_empty.setVisibility(View.VISIBLE);
        } else {
            ll_empty.setVisibility(View.GONE);
        }
    }
}